;(function() {
	'use strict';
	angular.module('services.resourcesWatchData.resourceMonitoring')
		.service('rm', RM)
		.factory('rmFactory', rmFactory)
		.service('rmService', RmService)
		.factory('rmFn', RmFn);

	function RM(rmFactory, rmService, rmFn) {
		this.rmFactory = rmFactory;
		this.rmService = rmService;
		this.rmFn = rmFn;
	}

	function rmFactory(Restangular) {
		return {
			queryAlarmNum: function(params, callBack) {
				Restangular.all('sas/resourceMonitoring/query').post(params).then(callBack);
			}
		};
	}

	function RmService() {
		var year = new Date().getFullYear();
		var month = new Date().getMonth();
		var day = new Date().getDate();
		this.SENDDATA = {
			timeType: '1',
			startTime: new Date(year, month, day - 6),
			endTime: new Date(year,month,day),
			equipmen : [],
			equipmenType: '1',
			type:'1'
		};
		this.TIMETYPE = [{
			timeTypeCode: '0',
			timeTypeName: '时'
		}, {
			timeTypeCode: '1',
			timeTypeName: '日'
		}];
		this.TYPE = [{
			code: '1',
			name: '软件'
		}, {
			code: '2',
			name: '硬件'
		}];
		this.STANDARDTYPE = [{
			name: '同比',
			code: '1'
		}, {
			name: '环比',
			code: '2'
		}];
		this.SOFTEQUIPMENTS = [{
			code: '1',
			name: '数据库'
		}, {
			code: '2',
			name: 'TOMCAT'
		}, {
			code: '3',
			name: '中间件'
		}];
		this.HARDEQUIPMENTS = [{
			code: '1',
			name: 'CPU'
		}, {
			code: '2',
			name: '磁盘'
		}];
	}

	function RmFn(CoreService) {
		return {
			getData : getData,
			fmtSendData : fmtSendData,
			getTitle : getTitle,
			changeTime : changeTime
		};
		function fmtSendData(sendData){
			var bind = {
				timeType: '',
				startTime: '',
				endTime: '',
				equipmen : [],
				equipmenType: '',
			};
			bind.equipmen = sendData.equipmen.map(function(eq){
				return eq.name;
			});
			if(sendData.timeType === '0'){
				bind.startTime = CoreService.dateFormat(sendData.startTime,'yyyy-MM-dd hh');
				bind.endTime = CoreService.dateFormat(sendData.endTime,'yyyy-MM-dd hh');
			}else {
				bind.startTime = CoreService.dateFormat(sendData.startTime,'yyyy-MM-dd');
				bind.endTime = CoreService.dateFormat(sendData.endTime,'yyyy-MM-dd');
			}
			bind.timeType = sendData.timeType;
			sendData.equipmenType === '1' ? bind.equipmenType = '软件' : bind.equipmenType = '硬件';
			return bind;
		}

		function echartsExamples(temp) {
			var option = {
				title: {
					text: temp.echartsTitleText,
					x: 'center'
				},
				animation: false,
				dataZoom: {
					show: true,
					realtime: true
				},
				grid:{
					x2:100
				},
				tooltip: {
					show: true
				},
				legend: {
					show: true,
					orient: 'vertical',
					data: temp.echartsLegend,
					x: 'right',
					y: 'center'
				},
				xAxis: [{
					type: 'category',
					data: temp.echartsXData
				}],
				yAxis: [{
					type: 'value'
				}],
				series:temp.echartsSeries
			};
			return option;
		}

		function getData(items,title,myEcharts) {
			myEcharts.clear();
			var seriesObj = {};
			var seriesData = {};
			var temp = {
				echartsXData: [],
				echartsLegend: [],
				echartsSeries: [],
				echartsTitleText : title
			};
			angular.forEach(items, function(item){
				if (temp.echartsXData.indexOf(item.time) === -1) {
					temp.echartsXData.push(item.time);
				}
				if (temp.echartsLegend.indexOf(item.equipmentName) === -1) {
					temp.echartsLegend.push(item.equipmentName);
				}
				seriesObj[item.equipmentName] = [];
				seriesData[item.equipmentName] = {};
			});
			angular.forEach(temp.echartsXData, function(x){
				angular.forEach(items, function(item){
					seriesData[item.equipmentName][x] = 0;
				});
			});
			angular.forEach(items, function(item){
				seriesData[item.equipmentName][item.time] = item.alarmNum;
			});
			angular.forEach(seriesData, function(s,index){
				angular.forEach(temp.echartsXData, function(x){
					seriesObj[index].push(s[x]);
				});
			});
			angular.forEach(temp.echartsLegend, function(el, i) {
				temp.echartsSeries[i] = {
					name: el,
					type: 'bar',
					data: seriesObj[el]
				};
			});
			var echartsOption = echartsExamples(temp);
			myEcharts.setOption(echartsOption);
		}
		function getTitle(send){
			var title = send.startTime+' _ '+send.endTime+send.equipmen.join(',')+' 告警量统计';
			return title;
		}

		function changeTime(timeType, sendData) {
			var year = new Date().getFullYear();
			var month = new Date().getMonth();
			var day = new Date().getDate();
			var hour = new Date().getHours();
			switch (timeType) {
				case '0':
					sendData.startTime = new Date(year, month, day, hour);
					sendData.endTime = new Date(year, month, day, hour);
					break;
				case '1':
					sendData.startTime = new Date(year, month, day - 6);
					sendData.endTime = new Date(year, month, day);
			}
		}
	}
})();